const qs = require('qs')

module.exports = {
  // 是否页面需要登陆
  needLogin: false,
  async handler (ctx) {
    // 获取 url 的 query 参数
    const {word, language} = ctx.request.body;
    let result = await ctx.fetch({
      uri: '/test/post',
      method: 'POST',
      // headers: {'Content-Type': 'application/json'}, // content-type 是 application/json
      // body: JSON.stringify({word, language})
      headers: {'Content-Type': 'application/x-www-form-urlencoded'}, // content-type 是 application/x-www-form-urlencoded
      body: qs.stringify({word, language})
    }, ctx)
    ctx.response.json(result, { error: false, message: 'success' });
    return result;
  },
  async catchError(ctx, error) {
    console.log(error);
    ctx.response.ajax(error, {error: true, message: error.message});
  },
}